/**********************************************************************************
 * Copyright (c) 2012 - 2012,  parallel, All right reserved
 * Title: com.adc.parallel.constants  SysConstants.java 
 * @author shen  shenqiao2012@163.com 
 *  2012-11-21 上午11:20:24 
 *********************************************************************************/


package com.adc.parallel.constants;

import java.io.File;
import java.io.IOException;
import java.util.Date;

import com.adc.quicktools.StringUtil;
import com.adc.quicktools.file.FileControl;
import com.softabar.sha4j.ShaUtil;


/**
 * 
 * Description: this component runtime constants
 * @author shen  shenqiao2012@163.com
 */
public class SysConstants {
	/**
	 * the global default value
	 */
	public final static long Default_Value=Short.MIN_VALUE;
	/**
	 * default config file name 
	 */
	private static String ConfigName="parallel-config.properties";
	/**
	 * default size of pool
	 */
	private final static int defaultPoolSize=10;
	/**
	 * size of pool
	 */
	private static int MaxPoolSize=defaultPoolSize;
	/**
	 * define execute time format
	 */
	public final static String EXEC_Time_Format="yyyy-MM-dd/HH:mm:ss.SSS";
	/**
	 * the type of receive task dto list
	 */
	public final static String ListType_RE="receive";
	/**
	 * the type of runtime task dto list
	 */
	public final static String ListType_RT="runtime";
	/**
	 * the type of wait task dto list
	 */
	public final static String ListType_WT="wait";
	/**
	 * the type of complete task dto list
	 */
	public final static String ListType_CP="complete";
	
	private final static String DaemonThreadNameFormat="%s-Daemon";
	
	/**
	 * the format of persistent filename
	 */
	private final static String PersistentFileNameFormat="persistentList-%s.ptf";
	/**
	 * the format of completed task record
	 */
	private final static String ProcessLogFormat="histories-%s.hrpd";
	/**
	 * persistent task dto format 
	 */
	private final static String persistentFormat="\t[%s,%s]\t%s";
	/**
	 * complete task dto toString format 
	 */
	private final static String completeFormat="[%s]-[%s]\t[result:%d]\t%s";
	/**
	 * the default executor thread return value
	 */
	public final static int Default_Exec_Return=(int) Default_Value;
	/**
	 * the global variable for main thread
	 */
	private static boolean ExitMain=false;
	
	/**
	 * init uid value
	 */
	private static long UID_INIT_VALUE=0L;
	
	/**
	 * 
	 * Description: obtain gid 
	 * author:  shen
	 * @return String
	 */
	public static String getGid(){
		long num=new Date().getTime()+UID_INIT_VALUE++;
		String gid = null;
		try {
			gid = ShaUtil.toSha256String(num+"").toUpperCase();
			System.out.println(gid+"\t"+num);
		} catch (IOException e) {
			e.printStackTrace();
		}
		return gid;
	}
	
	/**
	 * 
	 * Description: return formatted persistent string of task dto 
	 * author:  shen
	 * @param gid
	 * @param uid
	 * @param taskDataString
	 * @return String
	 */
	public static String getPersistentFormat(String gid,String uid,String taskDataString) {
		return String.format(persistentFormat, gid,uid,taskDataString);
	}
	
	/**
	 * @return the persistent file name format
	 */
	public static String getPersistentFileName(String identity) {
		return String.format(PersistentFileNameFormat, identity);
	}
	
	/**
	 * 
	 * Description: obtain process log name
	 * author:  shen
	 * @return String
	 */
	public static String getProcessLogName(){
		return String.format(ProcessLogFormat, FileControl.getCurrentTimeString("yyyyMMdd"));
	}
	
	/**
	 * 
	 * Description: obtain complete info 
	 * author:  shen
	 * @param stTime
	 * @param edTime
	 * @param result
	 * @param taskDtoInfo
	 * @return String
	 */
	public static String getCompleteFormat(String stTime,String edTime,int result,String taskDtoInfo) {
		return String.format(completeFormat, stTime,edTime,result,taskDtoInfo);
	}
	
	/**
	 * 
	 * Description: obtain daemon thread name
	 * author:  shen
	 * @param name
	 * @return String
	 */
	public static String getDaemonThreadName(String name) {
		return String.format(DaemonThreadNameFormat, name);
	}

	/**
	 * define sys app root base dir
	 */
	private static String appRoot;
	public static String getAppRoot() {
		if (!StringUtil.checkStringValue(appRoot)) {
			appRoot=new File(FileControl.getAppPath(SysConstants.class)).getAbsolutePath().replace("\\", "/").concat("/");
//			System.out.println(appRoot);
		}
		return appRoot;
	}
	
	
	/**
	 * @return the logBase
	 */
	private static String LogBase;
	public static String getLogBase() {
		if (!StringUtil.checkStringValue(LogBase)) {
			LogBase=getAppRoot().concat("log/");
//			System.out.println(LogBase);
		}
		return LogBase;
	}

	/**
	 * @return the exitMain
	 */
	public static boolean isExitMain() {
		return ExitMain;
	}

	/**
	 * @param exitMain the exitMain to set
	 */
	public static void setExitMain(boolean exitMain) {
		ExitMain = exitMain;
	}

	/**
	 * @return the maxPoolSize
	 */
	public static int getMaxPoolSize() {
		if (MaxPoolSize==defaultPoolSize) {
			
		}
		return MaxPoolSize;
	}

	/**
	 * @param maxPoolSize the maxPoolSize to set
	 */
	public static void setMaxPoolSize(int maxPoolSize) {
		MaxPoolSize = maxPoolSize;
	}

	/**
	 * @return the configName
	 */
	public static String getConfigName() {
		return ConfigName;
	}

	/**
	 * @param configName the configName to set
	 */
	public static void setConfigName(String configName) {
		ConfigName = configName;
	}
	
	
	
}
